<?php
/**
 * Created by PhpStorm.
 * User: 0000336
 * Date: 2019/3/15
 * Time: 12:24
 */

namespace App\Controller;

use App\Service\MessageGenerator;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Psr\Log\LoggerInterface;

class LuckyController extends AbstractController
{
    /**
     * @Route("/lucky/number", condition="")
     * @param LoggerInterface $logger
     * @return Response
     */
    public function number(LoggerInterface $logger)
    {
        $logger->info('hello');
//        $this->
        $number = rand(0, 100);
        return $this->render('lucky/number.html.twig', compact('number'));
//        return new Response("<html><body>Lucky number: ${number}</body></html>");
    }

    /**
     * @Route("/lucky/text")
     * @param MessageGenerator $messageGenerator
     * @return Response
     */
    public function text(MessageGenerator $messageGenerator, LoggerInterface $logger)
    {
        $body = $messageGenerator->getHappyMessage();
        $logger->warning($body);
//        return new JsonResponse(['data'=>$body]);
//        return new Response("<html><body>${body}</body></html>");

        return $this->render('base.html.twig', compact('body'));
    }

    /**
     * @Route("/lucky/test")
     * @return Response
     */
    public function test()
    {
        $url = $this->generateUrl('app_lucky_number');
        return new JsonResponse(['data'=>$url]);
    }

    /**
     * @Route("/lucky/r")
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
     */
    public function re()
    {
        return $this->redirectToRoute('app_lucky_number');
    }
}